package com.ygqh.baby.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.ao.SubscribeType;
import com.ygqh.baby.mapper.YgSubscribeMsgDetailMapper;
import com.ygqh.baby.po.YgSubscribeMsgDetail;
import com.ygqh.baby.po.YgSubscribeMsgDetailExample;
import com.ygqh.baby.service.YgSubscribeMsgDetailService;

@Service
public class YgSubscribeMsgDetailServiceImpl implements YgSubscribeMsgDetailService {

	@Autowired
	private YgSubscribeMsgDetailMapper ygSubscribeMsgDetailMapper;

	@Override
	public List<YgSubscribeMsgDetail> find() {
		YgSubscribeMsgDetailExample example = new YgSubscribeMsgDetailExample();
		example.createCriteria();
		return ygSubscribeMsgDetailMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgSubscribeMsgDetailExample example = new YgSubscribeMsgDetailExample();
		example.createCriteria();
		return ygSubscribeMsgDetailMapper.countByExample(example);
	}

	@Override
	public YgSubscribeMsgDetail findById(Long id) {
		return ygSubscribeMsgDetailMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgSubscribeMsgDetail ygSubscribeMsgDetail) {
		return ygSubscribeMsgDetailMapper.insertSelective(ygSubscribeMsgDetail);
	}

	@Override
	public int update(YgSubscribeMsgDetail ygSubscribeMsgDetail) {
		return ygSubscribeMsgDetailMapper.updateByPrimaryKeySelective(ygSubscribeMsgDetail);
	}

	@Override
	public ResultSet<YgSubscribeMsgDetail> search(QueryInfo queryInfo, String q) {
		List<YgSubscribeMsgDetail> list = ygSubscribeMsgDetailMapper.selectSuper(queryInfo, q);
		int count = ygSubscribeMsgDetailMapper.countSuper(q);
		ResultSet<YgSubscribeMsgDetail> resultSet = new ResultSet<YgSubscribeMsgDetail>(count, list);
		return resultSet;
	}

	@Override
	public int saveOrUpdate(YgSubscribeMsgDetail ygSubscribeMsgDetail) {
		int r = 0;
		if (ygSubscribeMsgDetail.getId() != null) {
			r = this.update(ygSubscribeMsgDetail);
		} else {
			r = this.save(ygSubscribeMsgDetail);
		}
		return r;
	}

	@Override
	public int addSubscribeMsg(String templateId, String openId, String scene) {
		String[] sceneArr = scene.split("-");
		// SubscribeType subscribeType = SubscribeType.Promotion;
		SubscribeType subscribeType = SubscribeType.valueOf(sceneArr[0]);
		int countByType = this.findCountByType(subscribeType, openId, sceneArr[1], false);
		if (countByType == 0) {
			Long productId = Long.parseLong(sceneArr[1]);
			YgSubscribeMsgDetail detail = new YgSubscribeMsgDetail(openId, templateId, sceneArr[1], subscribeType, productId, null);
			this.save(detail);
		}

		return 1;
	}

	@Override
	public int findCountByType(SubscribeType subscribeType, String openId, String scene, Boolean isSend) {
		return ygSubscribeMsgDetailMapper.selectCountByType(subscribeType, openId, scene, isSend);
	}

	@Override
	public List<YgSubscribeMsgDetail> findByType(SubscribeType subscribeType, String openId, String scene, Boolean isSend) {
		return ygSubscribeMsgDetailMapper.selectByType(subscribeType, openId, scene, isSend);
	}

	@Override
	public int updateSendStatusBatch(List<YgSubscribeMsgDetail> list) {
		if (CollectionUtils.isEmpty(list)) {
			return 0;
		}
		return ygSubscribeMsgDetailMapper.updateSendStatusBatch(list);
	}

	@Override
	public List<YgSubscribeMsgDetail> findByTypeAndRelationId(SubscribeType subscribeType, List<Long> relationIdList, Boolean isSend) {
		YgSubscribeMsgDetailExample example = new YgSubscribeMsgDetailExample();
		YgSubscribeMsgDetailExample.Criteria criteria = example.createCriteria();
		if (subscribeType != null) {
			criteria.andSubscribeTypeEqualTo(subscribeType.name());
		}
		if (!CollectionUtils.isEmpty(relationIdList)) {
			criteria.andRelationIdIn(relationIdList);
		}
		if (isSend != null) {
			criteria.andIsSendEqualTo(isSend);
		}
		return ygSubscribeMsgDetailMapper.selectByExample(example);
	}

}
